<template>
  <div class="UserUpdate">
    <div class="title">
      <span>管理员管理 | 管理员修改</span>
    </div>
    <div class="content">
      <h3>管理员修改</h3>
      <form @submit.prevent="update">
        <div class="items">
          <span>用户名称&nbsp;:</span>
          <input
            type="text"
            v-model="admin.username"
            @input="validateUsername"
            required
          />
          <div v-if="UnerrorMessage" ref="uNameError" class="error">
            {{ UnerrorMessage }}
          </div>
        </div>
        <div class="items">
          <span>用户密码&nbsp;:</span>
          <input
            type="password"
            v-model="admin.password"
            @input="validatePassword"
            required
          />
          <div v-if="PwerrorMessage" ref="pwdError" class="error">
            {{ PwerrorMessage }}
          </div>
        </div>
        <div class="items">
          <span>用户角色&nbsp;:</span>
          <input type="text" value="管理员" disabled />
        </div>
        <button type="submit" class="updateAdmin">提交修改</button>
      </form>
    </div>
  </div>
</template>
  
  <script>
import { updateUser } from "@/api/admin";
import { Message } from "element-ui";
export default {
  name: "userUpdate",
  data() {
    return {
      admin: {},
      usernamePass: true,
      passwordPass: true,
      UnerrorMessage: "用户名必须是 1 - 7 个汉字，不得输入特殊字符",
      PwerrorMessage:
        "密码长度应为 8 - 20 位，且必须包含至少一个大写字母、一个小写字母和一个数字",
    };
  },
  created() {
    this.admin = JSON.parse(this.$route.query.admin);
  },
  methods: {
    validatePassword() {
      const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,20}$/;
      if (!regex.test(this.admin.password) && this.admin.password !== "") {
        this.$refs.pwdError.style.opacity = 1;
        this.passwordPass = false;
      } else {
        this.passwordPass = true;
        this.$refs.pwdError.style.opacity = 0;
      }
    },
    validateUsername() {
      const regex = /^[\u4e00-\u9fa5]{1,8}$/;
      if (!regex.test(this.admin.username) && this.admin.username !== "") {
        this.$refs.uNameError.style.opacity = 1;
        this.usernamePass = false;
      } else {
        this.$refs.uNameError.style.opacity = 0;
        this.usernamePass = true;
      }
    },
    async update() {
      if (!this.passwordPass || !this.usernamePass)
        Message.error("用户名或密码格式错误！！！");
      else {
        let params = {
          id: this.admin.id,
          username: this.admin.username,
          password: this.admin.password,
          role: "admin",
        };
        await updateUser(params)
          .then((res) => {
            if (res.data.code === 200) {
              if (res.data.message === "Ok") {
                Message.success("管理员修改成功");
                this.$router.push({ name: "adminList" });
              } else {
                Message.error("管理员修改的名称已被占用，请重新修改！");
                setTimeout(() => {
                  window.location.reload();
                }, 1000);
              }
            }
          })
          .catch((error) => {
            console.log(error);
          });
      }
    },
  },
};
</script>
  
  <style scoped>
.UserUpdate {
  min-height: 100vh;
}
.title {
  display: flex;
  height: 50px;
  line-height: 50px;
  color: #666666;
  margin-bottom: 20px;
  font-size: 18px;
}
.content {
  display: flex;
  flex-direction: column;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  width: 500px;
  height: 500px;
  margin: 0px auto;
  background: rgba(232, 231, 231, 0.6);
  box-shadow: 10px 10px 25px #666666;
  border-radius: 10px;
}
h3 {
  display: block;
  text-align: center;
  color: #666666;
  height: 50px;
  margin-bottom: 40px;
}
.items {
  width: auto;
  height: 90px;
}
.items span {
  display: inline-block;
  width: 80px;
  text-align: left;
  color: #666666;
  font-size: 16px;
}
.items input {
  height: 40px;
  padding: 5px;
  border: 1px solid #c0c0c0;
  border-radius: 10px;
  width: 300px;
  font-size: 16px;
  color: #666666;
  vertical-align: middle;
}
.updateAdmin {
  background: rgba(42, 81, 77, 1);
  color: #fff;
  width: 400px;
  height: 35px;
  border-radius: 8px;
  margin: 10px auto 30px;
  border: 1px solid #215c3d;
  transition: all 0.3s;
}
.updateAdmin:hover {
  cursor: pointer;
  box-shadow: 15px 5px 25px #929493;
}
.error {
  color: rgb(176, 19, 19);
  font-size: 12px;
  width: 400px;
  padding: 8px 30px 0px 10px;
  opacity: 0;
  transition: all 0.5s;
}
</style>
